package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * Spu操作类
 * */
@RestController
@RequestMapping("/admin/product")
public class SpuManageController {
    @Autowired
    ManageService manageService;
    @Autowired
    SpuImageService spuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    /*
     * 分页查询SpuInfo
     * get  @RequestParam(value = "category3Id",required = true) == @RequestParam("category3Id") //此参数必须提交
     * */
    @GetMapping("{page}/{limit}")
    public Result<Page<SpuInfo>> getSpuInfoPage(
            @RequestParam("category3Id") Long category3Id,
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit) {
        Page<SpuInfo> spuInfoPage = new Page<SpuInfo>(page, limit);
        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id", category3Id);
        Page<SpuInfo> infoPage = manageService.page(spuInfoPage, wrapper);
        return Result.ok(infoPage);
    }

    /**
     * 保存SPU
     * 1、spu_info
     * 2、spu_image ： spu把后来所有sku要用的图片全部上传好，录入sku的时候sku自己选
     * 3、spu_sale_attr、spu_sale_attr_value
     *  先操作spu_sale_attr，保存以下值
     *      "baseSaleAttrId":"1",
     *      "saleAttrName":"选择颜色",
     *  再操作spu_sale_attr_value
     *      spuSaleAttrValueList:[
     *          {
     *              "baseSaleAttrId":"1",
     *              "saleAttrValueName":"黑色"
     *              }
     *      ]
     *   注意：spu_sale_attr、spu_sale_attr_value有冗余存储项，
     *   需要提前设置好值
     *  http://api.gmall.com/admin/product/saveSpuInfo
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        boolean b = manageService.bigSaveSpuInfo(spuInfo);
        return Result.ok();
    }

    /*
    * 查询spu对应的图片列表
    * */
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable("spuId") Long spuId){
        QueryWrapper<SpuImage> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id",spuId);
        List<SpuImage> list = spuImageService.list(wrapper);

        return Result.ok(list);
    }

    /**
     * 根据spuId获取商品销售属性列表
     *    每一组销售属性的组合，应该对应一个SKU
     *  颜色：黄、蓝
     *  内存：128,256
     *  SKU： 2*2=4：
     *   黄+128  黄+256
     *   蓝+128  蓝+256
     *   我们在录入sku的时候，只需要录入我们的组合
     *
     * @param spuId
     * @return
     */
    //http://api.gmall.com/admin/product/spuSaleAttrList/{spuId}
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId){
        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrService.getSpuAttrAndValue(spuId);
        return Result.ok(spuSaleAttrs);
    }
}
